<?php

function getTempFile() {
    return tempnam(APP_DATA_PATH, time());
}

function debug() {
    $trace = debug_backtrace();
    $count = count($trace);
    $last = $trace[$count - 1];
    return $last;
}

function testdebug() {
    print_r(debug_backtrace());
}

/**
 * 日志输出
 * @param type $msg
 */
function logformat($msg, $w = false, $p = true) {
    if (!$w && !$p && !DEFAULT_ECHOLOG && !DEFAULT_LOGLOG) {
        return false;
    }
    $last = debug();
    $traceInfo = $last['file'] . '|' . $last['line'] . '|' . $last['function'];
    $s = date('Y-m-d H:i:s') . '|' . $msg . '|' . $traceInfo . '|' . PHP_EOL;
    if ($w || DEFAULT_LOGLOG || (defined('UNDER_WEBSERVER') && defined('UNDER_WEBSERVER_ECHO_LOG'))) {
        $file = OUTPUT_LOGDIR . date('Y-m-d') . '.log';
        FcheckDir($file);
        file_put_contents($file, $s, 8);
    }
    if ($p || DEFAULT_ECHOLOG) {
        if (!defined('UNDER_WEBSERVER')) {
            echo $s;
        }
    }
}

/**
 * 获取FormHash
 * @return type
 */
function getFormHashValue() {
    return @file_get_contents(getFormHashFile());
}

/**
 * 设置FormHash
 * @param type $html
 * @return type
 */
function setFormHashValue($html) {
    $p = '#<input type="hidden" name="formhash" value="(.*)" />#isU';
    preg_match_all($p, $html, $d);
    $p = '#formhash=([a-zA-Z0-9]+)">#isU';
    if (!$d[1][0]) {
        preg_match_all($p, $html, $d);
    }
    file_put_contents(getFormHashFile(), $d[1][0]);
    return $d[1][0];
}

/**
 * 清空FormHash
 * @return boolean
 */
function clearFormHashFile() {
    @unlink(getFormHashFile());
    return true;
}

/**
 * FormHash所保存在的文件
 * @return type
 */
function getFormHashFile() {
    return APP_DATA_PATH . FORUM_DOMAIN . '_formhash.txt';
}

/**
 * 获取Cookie保存的文件
 * @return type
 */
function getCookieFile() {
    return APP_DATA_PATH . FORUM_DOMAIN . '_cookie.txt';
}

/**
 * 获取本地Cookie
 * @return type
 */
function getLocalCookie() {
    return @file_get_contents(getCookieFile());
}

/**
 * 获取Cookie数组
 * @return type
 */
function getLocalCookieArray() {
    $array = array();
    $s = file_get_contents(getCookieFile());
    $e = explode(';', $s);
    foreach ($e as $t) {
        $y = explode('=', $t);
        $array[$t[0]] = $t[1];
    }
    return $array;
}

/**
 * 清空本地Cookie
 * @return boolean
 */
function clearLocalCookie() {
    @unlink(getCookieFile());
    return true;
}

/**
 * 设置本地Cookie
 * @param type $headers
 * @return type
 */
function setLocalCookie($headers) {
    $strLocalCookie = @file_get_contents(getCookieFile());
    $arrLocalCookie = array();
    $arrLocalCookieOld = explode(';', $strLocalCookie);
    if (!empty($arrLocalCookieOld)) {
        foreach ($arrLocalCookieOld as $v) {
            $f = explode('=', $v);
            $arrLocalCookie[$f[0]] = $f[1];
        }
    }
    foreach ($headers as $header) {
        if (substr($header, 0, strlen('Set-Cookie:')) == 'Set-Cookie:') {
            $header = str_replace('Set-Cookie:', '', $header);
            $arrHeader = explode(';', $header);
            $cookieString = $arrHeader[0];
            $cookieArray = explode('=', $cookieString);
            $cookieArray[0] = trim($cookieArray[0]);
            $cookieArray[1] = trim($cookieArray[1]);
            $arrLocalCookie[$cookieArray[0]] = $cookieArray[1];
        }
    }
    $strCookieString = '';
    foreach ($arrLocalCookie as $k => $v) {
        $strCookieString.="{$k}={$v};";
    }
    file_put_contents(getCookieFile(), $strCookieString);
    return $strLocalCookie;
}

//检查并创建多级目录
function FcheckDir($path) {
    $pathArray = explode('/', $path);
    $nowPath = '';
    array_pop($pathArray);
    foreach ($pathArray as $key => $value) {
        if ('' == $value) {
            unset($pathArray[$key]);
        } else {
            if ($key == 0)
                $nowPath .= $value;
            else
                $nowPath .= '/' . $value;
            if (!is_dir($nowPath)) {
                if (!mkdir($nowPath, 0777))
                    return false;
            }
        }
    }
    return true;
}

function Number2Letter($number) {
    if (!is_numeric($number) || $number < 0 || strlen($number) == 0) {
        return false;
    }
    $string = '';
    $array = str_split(strval($number));
    foreach ($array as $int) {
        if (mt_rand(0, 10) > 6) {
            $string.=strval($int);
        } else {
            $rand = array();
            for ($i = 64; $i <= 90; $i++) {
                if (substr(strval($i), -1, 1) == $int) {
                    $rand[] = chr(intval($i));
                }
            }
            $key = mt_rand(0, count($rand) - 1);

            $string.=strtoupper($rand[$key]);
        }
    }
    return $string;
}

function Letter2Number($letter) {
    $letter = strtoupper($letter);
    $number = '';
    $array = str_split(strval($letter));
    if (empty($array)) {
        return false;
    }
    foreach ($array as $str) {
        if (is_numeric($str)) {
            $number.=strval($str);
        } else {
            $ascii = ord($str);
            if ($ascii > 90 || $ascii < 64) {
                return false;
            }
            $number.=strval(substr($ascii, -1, 1));
        }
    }
    return intval($number);
}

?>